package com.carl.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 时间工具类
 */
public class DateTimeUtil {

    private DateTimeUtil() {

    }

    private static final Logger logger = LoggerFactory.getLogger(DateTimeUtil.class);

    public static final String YYYY_MM_DDHHMMSS = "yyyy-MM-dd HH:mm:ss";

    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String HHMMSS = "HHmmss";

    public static final String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";

    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    public static final String YYYY_MM = "yyyy-MM";

    public static Date formateDate(String dateStr, String formateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(formateStr);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            logger.error("格式化日期失败,dateStr:{},formateStr:{}", dateStr, formateStr, e);
        }
        return null;
    }

    public static String formateDateStr(Date date, String formateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(formateStr);
        return sdf.format(date);
    }

    public static List<String> getBeforeNumDayYYYYMMDD(int num) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();


        List<LocalDate> dates = new ArrayList<>();
        dates.add(currentDate);

        for (int i = num-1; i > 0; i--) {
            LocalDate previousDate = currentDate.minusDays(i);
            dates.add(previousDate);
        }


        // 如果需要确保正序排列（从小到大），可以使用 Collections.sort
        // 由于 LocalDate 实现了 Comparable，默认就是正序排列
        Collections.sort(dates);

        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        List<String> strings = new ArrayList<>();
        for (LocalDate date : dates) {
            strings.add(date.format(formatter));
        }
        return strings;

    }
    public static List<String> getBeforeNumMonthYYYYMMDD(int num) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();


        List<YearMonth> months = new ArrayList<>();
        months.add(YearMonth.from(currentDate));

        for (int i = 1; i <= num-1; i++) {
            YearMonth previousMonth = YearMonth.from(currentDate.minusMonths(i));
            months.add(previousMonth);
        }

        // 对月份数据进行正序排序（从小到大）
        Collections.sort(months);

        // 定义月份格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        List<String> strings = new ArrayList<>();
        for (YearMonth month : months) {
            strings.add(month.format(formatter));
        }
        return strings;

    }

    public static List<String> getMonthFirstDayAndLastDayYYYYMMDDByYYYYMM(String monthStr) {

        ArrayList<String> strings = new ArrayList<>();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");

        // 示例日期字符串
        LocalDate firstDayOfMonth = null;
        LocalDate lastDayOfMonth = null;

        try {
            // 解析日期字符串为YearMonth对象
            YearMonth yearMonth = YearMonth.parse(monthStr, formatter);

            // 获取该月的第一天
            firstDayOfMonth = yearMonth.atDay(1);

            // 获取该月的最后一天，通过在当前月份的最后一天加1天的方式得到下一个月的第一天，然后减1
            lastDayOfMonth = yearMonth.plusMonths(1).atDay(1).minusDays(1);

            DateTimeFormatter formatterYYYYMMDD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String firstDay = firstDayOfMonth.format(formatterYYYYMMDD);
            String lastDay = lastDayOfMonth.format(formatterYYYYMMDD);
            strings.add(firstDay);
            strings.add(lastDay);
            return strings;

        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        }

    }

    public static List<String> getMonthFirstDayAndLastDayYYYYMMDD() {
        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 获取当前月份
        YearMonth yearMonth = YearMonth.from(today);

        // 获取当月第一天和最后一天
        LocalDate firstDayOfMonth = yearMonth.atDay(1);
        LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();

        // 格式化日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String firstDay = firstDayOfMonth.format(formatter);
        String lastDay = lastDayOfMonth.format(formatter);

        List<String> dataList = new ArrayList<>();
        dataList.add(firstDay);
        dataList.add(lastDay);

        return dataList;
    }


    public static int getMonthByDate(Date maintenanceTime) {
        // 将 Date 转换为 LocalDate
        LocalDate localDate = maintenanceTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        // 获取月份（月份从1开始，即1表示一月）
        int month = localDate.getMonthValue();
        return month;
    }
}
